package model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumMap;


public class Card implements CardInterface, Serializable
{
	private final CardSuit suit;
	
	private final CardRank rank;
	
	private final static EnumMap<CardSuit, EnumMap<CardRank, Card>> map = new EnumMap<CardSuit, EnumMap<CardRank, Card>>(CardSuit.class);
	
	private static final ArrayList<Card> prototypeCards;
	
	static
	{
	    for (CardSuit suit: CardSuit.values())
	    {
	    	EnumMap<CardRank, Card> suitMap = new EnumMap<CardRank, Card>(CardRank.class);
	    	
	        for (CardRank rank: CardRank.values())
	        {
	        	suitMap.put(rank, new Card(rank, suit));
	        }
	        
	        map.put(suit, suitMap);
	    }
	    
	    prototypeCards = new ArrayList<Card>();
		
		for (CardSuit suit: CardSuit.values())
		{
			for (CardRank rank: CardRank.values())
			{
				prototypeCards.add(Card.valueOf(rank, suit));
			}
		}
	}
	
	private Card(CardRank rank, CardSuit suit)
	{
		this.rank = rank;
		
		this.suit = suit;
	}
	
	public static Card valueOf(CardRank rank, CardSuit suit)
	{
		return map.get(suit).get(rank);
	}

	public static ArrayList<Card> newDeck()
	{
		return new ArrayList<Card>(prototypeCards);
	}
	
	public CardSuit getSuit()
	{
		return suit;
	}
	
	public CardRank getRank()
	{
		return rank;
	}
	
	public String toString()
	{
		return rank + " of " + suit;
	}
}
